// SPDX-License-Identifier: GPL-2.0
/*
 * LDA640 driver
 *
 * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
 *
 * V0.0X01.0X01 add poweron function.
 * V0.0X01.0X02 add enum_frame_interval function.
 * V0.0X01.0X03 add quick stream on/off
 */

#include <linux/clk.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
#include <linux/version.h>
#include <linux/rk-camera-module.h>
#include <media/media-entity.h>
#include <media/v4l2-async.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-subdev.h>

#define DRIVER_VERSION			KERNEL_VERSION(0, 0x01, 0x03)

#ifndef V4L2_CID_DIGITAL_GAIN
#define V4L2_CID_DIGITAL_GAIN		V4L2_CID_GAIN
#endif

/* 74.25Mhz */
#define LDA640_PIXEL_RATE		(74250 * 1000)
#define LDA640_XVCLK_FREQ		37125000

#define CHIP_ID				0xa
#define LDA640_REG_CHIP_ID		0x0112

#define LDA640_REG_CTRL_MODE		0x0100
#define LDA640_MODE_SW_STANDBY		0x0
#define LDA640_MODE_STREAMING		BIT(0)

#define LDA640_REG_EXPOSURE		0x0202
#define LDA640_EXPOSURE_MIN		0
#define LDA640_EXPOSURE_STEP		1
#define LDA640_VTS_MAX			0x465

#define LDA640_REG_ANALOG_GAIN		0x301e
#define ANALOG_GAIN_MIN			0x0
#define ANALOG_GAIN_MAX			0x78
#define ANALOG_GAIN_STEP		1
#define ANALOG_GAIN_DEFAULT		0x10

#define LDA640_REG_VTS			0x0340

#define LDA640_REG_ORIENTATION		0x0101
#define LDA640_ORIENTATION_H		0x1
#define LDA640_ORIENTATION_V		0x2

#define REG_NULL			0xFFFF

#define LDA640_REG_VALUE_08BIT		1
#define LDA640_REG_VALUE_16BIT		2
#define LDA640_REG_VALUE_24BIT		3

/* h_offs 35 v_offs 14 */
//#define PIX_FORMAT  MEDIA_BUS_FMT_UYVY8_2X8

#define PIX_FORMAT  MEDIA_BUS_FMT_UYVY8_1X16

#define LDA640_NAME			"LDA640"
#define LDA640_NUM_SUPPLIES  0

struct regval {
	u16 addr;
	u8 val;
};

struct LDA640_mode {
	u32 width;
	u32 height;
	struct v4l2_fract max_fps;
	u32 hts_def;
	u32 vts_def;
	u32 exp_def;
	const struct regval *reg_list;
};

struct LDA640 {
	struct i2c_client	*client;
	struct clk		*xvclk;
	struct gpio_desc	*reset_gpio;
	struct gpio_desc	*pwdn_gpio;
	struct regulator_bulk_data supplies[LDA640_NUM_SUPPLIES];

	struct v4l2_subdev	subdev;
	struct media_pad	pad;
	struct v4l2_ctrl_handler ctrl_handler;
	struct v4l2_ctrl	*exposure;
	struct v4l2_ctrl	*anal_gain;
	struct v4l2_ctrl	*digi_gain;
	struct v4l2_ctrl	*hblank;
	struct v4l2_ctrl	*vblank;
	struct v4l2_ctrl	*test_pattern;
	struct mutex		mutex;
	bool			streaming;
	bool			power_on;
	const struct LDA640_mode *cur_mode;
	u32			module_index;
	const char		*module_facing;
	const char		*module_name;
	const char		*len_name;
};

#define to_LDA640(sd) container_of(sd, struct LDA640, subdev)

/*
 * Xclk 37.125Mhz
 * Pclk 74.25Mhz
 * linelength 2200(0x44c * 2)
 * framelength 1125(0x465)
 * grabwindow_width 1920
 * grabwindow_height 1080
 * max_framerate 30fps
 * dvp bt656 10bit
 */

static const struct regval LDA640_regs[] = {
	{REG_NULL, 0x00},
};


static const struct LDA640_mode supported_modes[] = {
	{
		.width = 640,
		.height = 512,
		.max_fps = {
			.numerator = 10000,
			.denominator = 250000,
		},
	        .exp_def = 1546,
		.hts_def = 640,
		.vts_def = 512,
		.reg_list = LDA640_regs,
	}
};



/* Write registers up to 4 at a time */
static int LDA640_write_reg(struct i2c_client *client, u16 reg,
			    int len, u32 val)
{
	
	return 0;
}

static int LDA640_write_array(struct i2c_client *client,
			      const struct regval *regs)
{
	int ret = 0;
	return ret;
}





static int LDA640_set_fmt(struct v4l2_subdev *sd,
			  struct v4l2_subdev_pad_config *cfg,
			  struct v4l2_subdev_format *fmt)
{
	struct LDA640 *LDA640 = to_LDA640(sd);
	const struct LDA640_mode *mode;
	s64 h_blank, vblank_def;

	mutex_lock(&LDA640->mutex);

	mode = &supported_modes[0];
	fmt->format.code = PIX_FORMAT;
	fmt->format.width = mode->width;
	fmt->format.height = mode->height;
	fmt->format.field = V4L2_FIELD_NONE;
	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
		*v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
#else
		mutex_unlock(&LDA640->mutex);
		return -ENOTTY;
#endif
	} 
	else
	{
		LDA640->cur_mode = mode;
		h_blank = mode->hts_def - mode->width;
		__v4l2_ctrl_modify_range(LDA640->hblank, h_blank,
					 h_blank, 1, h_blank);
		vblank_def = mode->vts_def - mode->height;
		__v4l2_ctrl_modify_range(LDA640->vblank, vblank_def,
					 LDA640_VTS_MAX - mode->height,
					 1, vblank_def);
	}

	mutex_unlock(&LDA640->mutex);

	return 0;
}

static int LDA640_get_fmt(struct v4l2_subdev *sd,
			  struct v4l2_subdev_pad_config *cfg,
			  struct v4l2_subdev_format *fmt)
{
	struct LDA640 *LDA640 = to_LDA640(sd);
	const struct LDA640_mode *mode = LDA640->cur_mode;

	mutex_lock(&LDA640->mutex);
        fmt->format.width = mode->width;
	fmt->format.height = mode->height;
	fmt->format.code = PIX_FORMAT;
	fmt->format.field = V4L2_FIELD_NONE;
	
	mutex_unlock(&LDA640->mutex);

	return 0;
}

static int LDA640_enum_mbus_code(struct v4l2_subdev *sd,
				 struct v4l2_subdev_pad_config *cfg,
				 struct v4l2_subdev_mbus_code_enum *code)
{
	if (code->index != 0)
		return -EINVAL;
	code->code = PIX_FORMAT;

	return 0;
}

static int LDA640_enum_frame_sizes(struct v4l2_subdev *sd,
				   struct v4l2_subdev_pad_config *cfg,
				   struct v4l2_subdev_frame_size_enum *fse)
{
	if (fse->index >= ARRAY_SIZE(supported_modes))
		return -EINVAL;

	if (fse->code != PIX_FORMAT)
		return -EINVAL;

	fse->min_width  = supported_modes[fse->index].width;
	fse->max_width  = supported_modes[fse->index].width;
	fse->max_height = supported_modes[fse->index].height;
	fse->min_height = supported_modes[fse->index].height;

	return 0;
}



static void LDA640_get_module_inf(struct LDA640 *LDA640,
				  struct rkmodule_inf *inf)
{
	memset(inf, 0, sizeof(*inf));
	strlcpy(inf->base.sensor, LDA640_NAME, sizeof(inf->base.sensor));
	strlcpy(inf->base.module, LDA640->module_name,
		sizeof(inf->base.module));
	strlcpy(inf->base.lens, LDA640->len_name, sizeof(inf->base.lens));
}



static long LDA640_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
{
	struct LDA640 *LDA640 = to_LDA640(sd);
	long ret = 0;
	u32 stream = 0;

	switch (cmd) {
	case RKMODULE_GET_MODULE_INFO:
		LDA640_get_module_inf(LDA640, (struct rkmodule_inf *)arg);
		break;

				
	case RKMODULE_SET_QUICK_STREAM:

		stream = *((u32 *)arg);

	    break;

	case RKMODULE_GET_BT656_INTF_TYPE:
		*(__u32 *)arg = BT656_SONY_RAW;
		break;

	default:
		ret = -ENOIOCTLCMD;
		break;
	}

	return ret;
}

#ifdef CONFIG_COMPAT
static long LDA640_compat_ioctl32(struct v4l2_subdev *sd,
				  unsigned int cmd, unsigned long arg)
{
	void __user *up = compat_ptr(arg);
	struct rkmodule_inf *inf;
	struct rkmodule_awb_cfg *cfg;
	__u32 intf;
	long ret;
	u32 stream = 0;

	switch (cmd) {
	case RKMODULE_GET_MODULE_INFO:
		inf = kzalloc(sizeof(*inf), GFP_KERNEL);
		if (!inf) {
			ret = -ENOMEM;
			return ret;
		}

		ret = LDA640_ioctl(sd, cmd, inf);
		if (!ret)
			ret = copy_to_user(up, inf, sizeof(*inf));
		kfree(inf);
		break;
	case RKMODULE_AWB_CFG:
		cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
		if (!cfg) {
			ret = -ENOMEM;
			return ret;
		}

		ret = copy_from_user(cfg, up, sizeof(*cfg));
		if (!ret)
			ret = LDA640_ioctl(sd, cmd, cfg);
		kfree(cfg);
		break;

	case RKMODULE_SET_QUICK_STREAM:
		ret = copy_from_user(&stream, up, sizeof(u32));
		if (!ret)
			ret = LDA640_ioctl(sd, cmd, &stream);
		break;

	case RKMODULE_GET_BT656_INTF_TYPE:
		intf = BT656_SONY_RAW;

		ret = copy_to_user(up, &intf, sizeof(intf));
		break;
	default:
		ret = -ENOIOCTLCMD;
		break;
	}

	return ret;
}
#endif

static int __LDA640_start_stream(struct LDA640 *LDA640)
{
	int ret;

	ret = LDA640_write_array(LDA640->client, LDA640->cur_mode->reg_list);
	if (ret)
		return ret;

	/* In case these controls are set before streaming */
	mutex_unlock(&LDA640->mutex);
	ret = v4l2_ctrl_handler_setup(&LDA640->ctrl_handler);
	mutex_lock(&LDA640->mutex);
	if (ret)
		return ret;

	return LDA640_write_reg(LDA640->client, LDA640_REG_CTRL_MODE,
				LDA640_REG_VALUE_08BIT, LDA640_MODE_STREAMING);
}

static int __LDA640_stop_stream(struct LDA640 *LDA640)
{
	return LDA640_write_reg(LDA640->client, LDA640_REG_CTRL_MODE,
				LDA640_REG_VALUE_08BIT, LDA640_MODE_SW_STANDBY);
}

static int LDA640_s_stream(struct v4l2_subdev *sd, int on)
{
	struct LDA640 *LDA640 = to_LDA640(sd);
	struct i2c_client *client = LDA640->client;
	int ret = 0;

	mutex_lock(&LDA640->mutex);
	on = !!on;
	if (on == LDA640->streaming)
		goto unlock_and_return;

	if (on) {
		ret = pm_runtime_get_sync(&client->dev);
		if (ret < 0) {
			pm_runtime_put_noidle(&client->dev);
			goto unlock_and_return;
		}

		ret = __LDA640_start_stream(LDA640);
		if (ret) {
			v4l2_err(sd, "start stream failed while write regs\n");
			pm_runtime_put(&client->dev);
			goto unlock_and_return;
		}
	} else {
		__LDA640_stop_stream(LDA640);
		pm_runtime_put(&client->dev);
	}

	LDA640->streaming = on;
unlock_and_return:
	mutex_unlock(&LDA640->mutex);

	return ret;
}

static int LDA640_g_frame_interval(struct v4l2_subdev *sd,
				   struct v4l2_subdev_frame_interval *fi)
{
	struct LDA640 *LDA640 = to_LDA640(sd);
	const struct LDA640_mode *mode = LDA640->cur_mode;

	mutex_lock(&LDA640->mutex);
	fi->interval = mode->max_fps;
	mutex_unlock(&LDA640->mutex);

	return 0;
}

static int LDA640_s_power(struct v4l2_subdev *sd, int on)
{
	struct LDA640 *LDA640 = to_LDA640(sd);

	int ret = 0;

	mutex_lock(&LDA640->mutex);
	mutex_unlock(&LDA640->mutex);

	return ret;
}

/* Calculate the delay in us by clock rate and clock cycles */
static inline u32 LDA640_cal_delay(u32 cycles)
{
	return DIV_ROUND_UP(cycles, LDA640_XVCLK_FREQ / 1000 / 1000);
}

static int __LDA640_power_on(struct LDA640 *LDA640)
{
	int ret;
	u32  delay_us;
	struct device *dev = &LDA640->client->dev;

	ret = clk_set_rate(LDA640->xvclk, LDA640_XVCLK_FREQ);
	if (ret < 0) {
		dev_err(dev, "Failed to set xvclk rate (%d)\n",
			LDA640_XVCLK_FREQ);
		return ret;
	}
	if (clk_get_rate(LDA640->xvclk) != LDA640_XVCLK_FREQ)
		dev_warn(dev, "xvclk mismatched, modes are based on %d\n",
			LDA640_XVCLK_FREQ);
	ret = clk_prepare_enable(LDA640->xvclk);
	if (ret < 0) {
		dev_err(dev, "Failed to enable xvclk\n");
		return ret;
	}
	
	/* 8192 cycles prior to first SCCB transaction */
	delay_us = LDA640_cal_delay(8192);
	usleep_range(delay_us, delay_us * 2);

	return 0;


}

static void __LDA640_power_off(struct LDA640 *LDA640)
{

	clk_disable_unprepare(LDA640->xvclk);
}

static int LDA640_runtime_resume(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct LDA640 *LDA640 = to_LDA640(sd);

	return __LDA640_power_on(LDA640);
}

static int LDA640_runtime_suspend(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct LDA640 *LDA640 = to_LDA640(sd);

	__LDA640_power_off(LDA640);

	return 0;
}

#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
static int LDA640_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
{
	struct LDA640 *LDA640 = to_LDA640(sd);
	struct v4l2_mbus_framefmt *try_fmt =
				v4l2_subdev_get_try_format(sd, fh->pad, 0);
	const struct LDA640_mode *def_mode = &supported_modes[0];

	mutex_lock(&LDA640->mutex);
	/* Initialize try_fmt */
	try_fmt->width = def_mode->width;
	try_fmt->height = def_mode->height;
	try_fmt->code = PIX_FORMAT;
	try_fmt->field = V4L2_FIELD_NONE;
	mutex_unlock(&LDA640->mutex);
	/* No crop or compose */

	return 0;
}
#endif

static int LDA640_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad_id,
				struct v4l2_mbus_config *config)
{
	config->type = V4L2_MBUS_BT656;
		
	config->flags = RKMODULE_CAMERA_BT656_CHANNELS |
			V4L2_MBUS_PCLK_SAMPLE_RISING;
			
	return 0;
}

static int LDA640_enum_frame_interval(struct v4l2_subdev *sd,
				       struct v4l2_subdev_pad_config *cfg,
				       struct v4l2_subdev_frame_interval_enum *fie)
{
	if (fie->index >= ARRAY_SIZE(supported_modes))
		return -EINVAL;

	if (fie->code != PIX_FORMAT)
		return -EINVAL;

	fie->width = supported_modes[fie->index].width;
	fie->height = supported_modes[fie->index].height;
	fie->interval = supported_modes[fie->index].max_fps;
	return 0;
}

static const struct dev_pm_ops LDA640_pm_ops = {
	SET_RUNTIME_PM_OPS(LDA640_runtime_suspend,
			   LDA640_runtime_resume, NULL)
};

#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
static const struct v4l2_subdev_internal_ops LDA640_internal_ops = {
	.open = LDA640_open,
};
#endif

static const struct v4l2_subdev_core_ops LDA640_core_ops = {
	.s_power = LDA640_s_power,
	.ioctl = LDA640_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl32 = LDA640_compat_ioctl32,
#endif
};


static int LDA640_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
{

//	*std = V4L2_STD_PAL;
	*std = V4L2_STD_ATSC;

	return 0;
}


static const struct v4l2_subdev_video_ops LDA640_video_ops = {
	.s_stream = LDA640_s_stream,
	.g_frame_interval = LDA640_g_frame_interval,
	.querystd = LDA640_querystd,
};

static const struct v4l2_subdev_pad_ops LDA640_pad_ops = {
	.enum_mbus_code = LDA640_enum_mbus_code,
	.enum_frame_size = LDA640_enum_frame_sizes,
	.enum_frame_interval = LDA640_enum_frame_interval,
	.get_fmt = LDA640_get_fmt,
	.set_fmt = LDA640_set_fmt,
	.get_mbus_config = LDA640_g_mbus_config,
};

static const struct v4l2_subdev_ops LDA640_subdev_ops = {
	.core	= &LDA640_core_ops,
	.video	= &LDA640_video_ops,
	.pad	= &LDA640_pad_ops,
};

static int LDA640_set_ctrl(struct v4l2_ctrl *ctrl)
{
	struct LDA640 *LDA640 = container_of(ctrl->handler,
					     struct LDA640, ctrl_handler);
	struct i2c_client *client = LDA640->client;
	int ret = 0;

	if (!pm_runtime_get_if_in_use(&client->dev))
		return 0;

	switch (ctrl->id) {
	case V4L2_CID_EXPOSURE:
		ret = LDA640_write_reg(LDA640->client, LDA640_REG_EXPOSURE,
				       LDA640_REG_VALUE_16BIT, ctrl->val);
		break;
	case V4L2_CID_ANALOGUE_GAIN:
		ret = LDA640_write_reg(LDA640->client, LDA640_REG_ANALOG_GAIN,
				       LDA640_REG_VALUE_08BIT, ctrl->val);
		break;
	case V4L2_CID_VBLANK:
		ret = LDA640_write_reg(LDA640->client, LDA640_REG_VTS,
				       LDA640_REG_VALUE_16BIT,
				       ctrl->val + LDA640->cur_mode->height);
		break;
	case V4L2_CID_TEST_PATTERN:
		break;
	default:
		dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
			 __func__, ctrl->id, ctrl->val);
		break;
	}

	pm_runtime_put(&client->dev);

	return ret;
}

static const struct v4l2_ctrl_ops LDA640_ctrl_ops = {
	.s_ctrl = LDA640_set_ctrl,
};

static int LDA640_initialize_controls(struct LDA640 *LDA640)
{
       struct v4l2_ctrl_handler *handler;
	int ret;
	int pixrate = 640*512;

	handler = &LDA640->ctrl_handler;
	ret = v4l2_ctrl_handler_init(handler, 2);
	if (ret)
		return ret;
	handler->lock = &LDA640->mutex;

	v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
			  0, pixrate, 1, pixrate);

	if (handler->error) {
		ret = handler->error;
		dev_err(&LDA640->client->dev,
			"Failed to init controls(%d)\n", ret);
		goto err_free_handler;
	}

	LDA640->subdev.ctrl_handler = handler;

	return 0;

err_free_handler:
	v4l2_ctrl_handler_free(handler);

	return 0;

}

static int LDA640_check_sensor_id(struct LDA640 *LDA640,
				  struct i2c_client *client)
{

	return 0;
}

static int LDA640_configure_regulators(struct LDA640 *LDA640)
{
	return 0;
}

static int LDA640_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	struct device *dev = &client->dev;
	struct device_node *node = dev->of_node;
	struct LDA640 *LDA640;
	struct v4l2_subdev *sd;
	char facing[2];
	int ret;

	dev_info(dev, "driver version: %02x.%02x.%02x",
		DRIVER_VERSION >> 16,
		(DRIVER_VERSION & 0xff00) >> 8,
		DRIVER_VERSION & 0x00ff);

	LDA640 = devm_kzalloc(dev, sizeof(*LDA640), GFP_KERNEL);
	if (!LDA640)
		return -ENOMEM;

	ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
				   &LDA640->module_index);
	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
				       &LDA640->module_facing);
	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
				       &LDA640->module_name);
	ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
				       &LDA640->len_name);
	if (ret) {
		dev_err(dev, "could not get module information!\n");
		return -EINVAL;
	}

	LDA640->client = client;
	LDA640->cur_mode = &supported_modes[0];

	LDA640->xvclk = devm_clk_get(dev, "xvclk");
	if (IS_ERR(LDA640->xvclk)) {
		dev_err(dev, "Failed to get xvclk\n");
		return -EINVAL;
	}

	ret = LDA640_configure_regulators(LDA640);
	if (ret) {
		dev_err(dev, "Failed to get power regulators\n");
		return ret;
	}

	mutex_init(&LDA640->mutex);

	sd = &LDA640->subdev;
	v4l2_i2c_subdev_init(sd, client, &LDA640_subdev_ops);
	ret = LDA640_initialize_controls(LDA640);
	if (ret)
		goto err_destroy_mutex;

	ret = __LDA640_power_on(LDA640);
	if (ret)
		goto err_free_handler;

	ret = LDA640_check_sensor_id(LDA640, client);
	if (ret)
		goto err_power_off;

#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
	sd->internal_ops = &LDA640_internal_ops;
	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
		     V4L2_SUBDEV_FL_HAS_EVENTS;
#endif
#if defined(CONFIG_MEDIA_CONTROLLER)
	LDA640->pad.flags = MEDIA_PAD_FL_SOURCE;
	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
	ret = media_entity_pads_init(&sd->entity, 1, &LDA640->pad);
	if (ret < 0)
		goto err_power_off;
#endif

	memset(facing, 0, sizeof(facing));
    facing[0] = 'f';

	snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
		 LDA640->module_index, facing,
		 LDA640_NAME, dev_name(sd->dev));
	ret = v4l2_async_register_subdev_sensor_common(sd);
	if (ret) {
		dev_err(dev, "v4l2 async register subdev failed\n");
		goto err_clean_entity;
	}

	pm_runtime_set_active(dev);
	pm_runtime_enable(dev);
	pm_runtime_idle(dev);

	return 0;

err_clean_entity:
#if defined(CONFIG_MEDIA_CONTROLLER)
	media_entity_cleanup(&sd->entity);
#endif
err_power_off:
	__LDA640_power_off(LDA640);
err_free_handler:
	v4l2_ctrl_handler_free(&LDA640->ctrl_handler);
err_destroy_mutex:
	mutex_destroy(&LDA640->mutex);

	return ret;
}

static int LDA640_remove(struct i2c_client *client)
{
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct LDA640 *LDA640 = to_LDA640(sd);

	v4l2_async_unregister_subdev(sd);
#if defined(CONFIG_MEDIA_CONTROLLER)
	media_entity_cleanup(&sd->entity);
#endif
	v4l2_ctrl_handler_free(&LDA640->ctrl_handler);
	mutex_destroy(&LDA640->mutex);

	pm_runtime_disable(&client->dev);
	if (!pm_runtime_status_suspended(&client->dev))
		__LDA640_power_off(LDA640);
	pm_runtime_set_suspended(&client->dev);

	return 0;
}

#if IS_ENABLED(CONFIG_OF)
static const struct of_device_id LDA640_of_match[] = {
	{ .compatible = "lda640-v4l2" },
	{},
};
MODULE_DEVICE_TABLE(of, LDA640_of_match);
#endif

static const struct i2c_device_id LDA640_match_id[] = {
	{ "lda640-v4l2", 0 },
	{ },
};

static struct i2c_driver LDA640_i2c_driver = {
	.driver = {
		.name = LDA640_NAME,
		.pm = &LDA640_pm_ops,
		.of_match_table = of_match_ptr(LDA640_of_match),
	},
	.probe		= &LDA640_probe,
	.remove		= &LDA640_remove,
	.id_table	= LDA640_match_id,
};

static int __init sensor_mod_init(void)
{
	return i2c_add_driver(&LDA640_i2c_driver);
}

static void __exit sensor_mod_exit(void)
{
	i2c_del_driver(&LDA640_i2c_driver);
}

device_initcall_sync(sensor_mod_init);
module_exit(sensor_mod_exit);

MODULE_DESCRIPTION("Sony LDA640 sensor driver");
MODULE_LICENSE("GPL v2");
